package com.lanyou.esb.wsdl.test;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.wsdl.Binding;
import javax.wsdl.BindingOperation;
import javax.wsdl.Definition;
import javax.wsdl.Operation;
import javax.wsdl.Port;
import javax.wsdl.PortType;
import javax.wsdl.Service;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.xml.namespace.QName;

import org.exolab.castor.xml.schema.SimpleTypesFactory;

import com.lanyou.esb.wsdl.domain.OperationInfo;
import com.lanyou.esb.wsdl.domain.ParameterInfo;
import com.lanyou.esb.wsdl.domain.ServiceInfo;
import com.lanyou.esb.wsdl.util.ComponentBuilder;


public class WSClient {
	static WSDLFactory wsdlFactory = null;

	static SimpleTypesFactory simpleTypesFactory = null;

	private Vector wsdlTypes = new Vector();

	public final static String DEFAULT_SOAP_ENCODING_STYLE = "http://schemas.xmlsoap.org/soap/encoding/";

	public WSClient() {
		try {
			wsdlFactory = WSDLFactory.newInstance();
			simpleTypesFactory = new SimpleTypesFactory();
		} catch (Throwable t) {
			System.err.println(t.getMessage());
		}
	}
	
	public static void testWSDL4J() {
		int i = 0, j = 0;

		try {
			ComponentBuilder builder = new ComponentBuilder();
			ServiceInfo serviceInfo = new ServiceInfo();
			
			//String wsdllocation = "http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";
			//String wsdllocation = "http://172.25.185.109/cxfserver/deptws?wsdl";
			//String wsdllocation = "http://172.25.185.109/hellowsserver/hellows?sdl";
			String wsdllocation = "http://weixin.szlanyou.com:804/webservice/DataService.svc?wsdl";
			/*String wsdllocation = "http://www.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";*/
			serviceInfo.setWsdllocation(wsdllocation);
			
			
			/*wsdlFactory = WSDLFactory.newInstance();
			simpleTypesFactory = new SimpleTypesFactory();
			WSDLReader reader=wsdlFactory.newWSDLReader();
			Definition def=reader.readWSDL("RetriveData", serviceInfo.getWsdllocation());
			System.out.println(def.getTargetNamespace());
			System.out.println(def.getQName().getPrefix());
			System.out.println(def.getQName().getNamespaceURI());
			System.out.println(def.getQName().getLocalPart());
			Map portType = def.getPortTypes().values().iterator();*/
			
			
			serviceInfo = builder.buildserviceinformation(serviceInfo);
			System.out.println("");
			Iterator iter = serviceInfo.getOperations();
			System.out.println("现在可以查看远端Web服务对象的有关情况了(对应本地Web服务类,ServiceInfo)");
			System.out.println(serviceInfo.getName() + "提供的操作有:");
			while (iter.hasNext()) {
				i++;
				OperationInfo oper = (OperationInfo) iter.next();
				System.out.println("");
				System.out.println("操作:" + i + " " + oper.getTargetMethodName());
				System.out.println("操作:" + i + " " + oper.getSoapActionURI());
				List inps = oper.getInparameters();
				List outps = oper.getOutparameters();
				if (inps.size() == 0) {
					System.out.println("此操作所需的输入参数为:");
					System.out.println("执行此操作不需要输入任何参数!");
				} else {
					System.out.println("此操作所需的输入参数为:");
					for (Iterator iterator1 = inps.iterator(); iterator1.hasNext();) {
						ParameterInfo element = (ParameterInfo) iterator1.next();
						System.out.println("参数名为:" + element.getName());
						System.out.println("参数类型为:" + element.getKind());
					}
				}
				if (outps.size() == 0) {
					System.out.println("执行此操作不返回任何参数!");
				} else {
					System.out.println("此操作的输出参数为:");
					for (Iterator iterator2 = outps.iterator(); iterator2.hasNext();) {
						ParameterInfo element = (ParameterInfo) iterator2.next();
						System.out.println("参数名:" + element.getName());
						System.out.println("类型为:" + element.getKind());
					}
				}
				System.out.println("");
			}
		}

		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
		}
	}

	/*public static void main(String[] args) {
		WSClient.testWSDL4J();
	}*/
	
	

}
